package tab

import (
	"fmt"
	"strings"

	"gopkg.in/xmlpath.v2"

	"code.google.com/awwwe-browser/auralvm/conversation"
	"code.google.com/awwwe-browser/auralvm/param"
	"code.google.com/awwwe-browser/auralvm/setting"
	"code.google.com/awwwe-browser/data"
	"code.google.com/awwwe-browser/scrub"
	"code.google.com/awwwe-browser/vois"
	"code.google.com/awwwe-browser/xpath"
)

// _buildContentConversationArticleLinks initializes the data for tab contents
// each option is a link from the article.
func _buildContentConversationArticleLinks(scrubber *scrub.Scrubber, tree *xmlpath.Node, content *vois.VOISContent, tabkey, contentindex int) ([]*conversation.Conversation, bool) {
	var conv *conversation.Conversation
	var conversations []*conversation.Conversation
	var line, title, ctype string
	var ok bool
	conv = conversation.NewConversation()
	conversations = make([]*conversation.Conversation, 0, 1)
	ok = (content.ContentType == vois.ArticleContentType) && xpath.IsTrueString(content.Condition, tree)
	if ok {
		// never speak at
		_setNeverSpeakAt(scrubber, tree, content.NeverSpeakAt)
		// title
		line = param.EndSentence(buildSpeechLine(scrubber, tree, content.TitleAt))
		line = strings.Replace(line, data.VXMLLongPauseString, "", -1)
		line = strings.Trim(line, data.WhiteSpaceString)
		if len(line) > 0 {
			title = line
		} else if len(content.Title) > 0 {
			title = content.Title
		} else {
			ok = false
		}
		if ok {
			ctype = content.TypeToVoice()
			conv = conversation.NewConversation()
			conv.BuildSpeechArticleLinks(scrubber, tree, content.OnlySpeakAt, tabkey, "tab", "listen", contentindex)
			conv.Type = conversation.ConversationTypeLinks
			conv.Title = fmt.Sprintf("The links in the %s titled %s", ctype, title)
			conv.Header.Append(fmt.Sprintf("These are the links in the %s titled %s", ctype, title))
			conv.Footer = fmt.Sprintf("That is the end of the links in the %s titled %s", ctype, title)
			if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
				// a lot of help or a little help.
				// add helpful
				conv.Helpful = fmt.Sprintf("I will speak the links in the %s to you. After I speak a link to you, say yes, if you want to listen to the resource at that link. Or say, yes new tab, if you want to listen to the resource at that link in a new tab.", ctype)
			}
			if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
				// a lot of help
				// add more helpful
				conv.MoreHelpful = "You may say, yes, yes new tab, back, restart, finish, help, pause, or quit."
			}
			ok = len(conv.Options) > 0
			if ok {
				conversations = append(conversations, conv)
			}
		}
	}
	return conversations, ok
}

// _buildContentConversationArticleVideos builds the conversation allowing the user to select a video from the article
//   followed by each conversation which plays the video.
// initializes data for Tab.Options.
func _buildContentConversationArticleVideos(scrubber *scrub.Scrubber, tree *xmlpath.Node, content *vois.VOISContent, tabkey int, sourceobject, sourcemethod string, sourcecontentindex int) ([]*conversation.Conversation, bool) {
	var conv *conversation.Conversation
	var conversations []*conversation.Conversation
	var line, title, ctype string
	var ok bool
	var length int
	conv = conversation.NewConversation()
	conversations = make([]*conversation.Conversation, 0, 1)
	ok = (content.ContentType == vois.ArticleContentType) && xpath.IsTrueString(content.Condition, tree)
	if ok {
		// never speak at
		_setNeverSpeakAt(scrubber, tree, content.NeverSpeakAt)
		// title
		line = param.EndSentence(buildSpeechLine(scrubber, tree, content.TitleAt))
		line = strings.Replace(line, data.VXMLLongPauseString, "", -1)
		line = strings.Trim(line, data.WhiteSpaceString)
		if len(line) > 0 {
			title = line
		} else if len(content.Title) > 0 {
			title = content.Title
		} else {
			ok = false
		}
		if ok {
			conv.BuildSpeechArticleVideos(scrubber, tree, content.OnlySpeakAt, tabkey, "tab", "listen", sourcecontentindex)
			length = len(conv.Videos)
			ok = length > 0
			if ok {
				conv.Type = conversation.ConversationTypeVideos
				conversations = append(conversations, conv)
				ctype = content.TypeToVoice()
				conv.Title = fmt.Sprintf("The videos in the %s titled %s", ctype, title)
				if length == 1 {
					conv.Header.Append(fmt.Sprintf("This is the video in the %s titled %s", ctype, title))
					conv.Footer = fmt.Sprintf("That is the end of the videos in the %s titled %s", ctype, title)
				} else {
					conv.Header.Append(fmt.Sprintf("These are the videos in the %s titled %s", ctype, title))
					conv.Footer = fmt.Sprintf("That is the end of the videoss in the %s titled %s", ctype, title)
					if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
						// a lot of help or a little help.
						// add helpful
						conv.Helpful = fmt.Sprintf("I will speak the links in the %s to you. After I speak a link to you, say yes, if you want to listen to the resource at that link. Or say, yes new tab, if you want to listen to the resource at that link in a new tab.", ctype)
					}
					if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
						// a lot of help
						// add more helpful
						conv.MoreHelpful = "You may say, yes, yes new tab, back, restart, finish, help, pause, or quit."
					}
				}
			}
		}
	}
	return conversations, ok
}

// _buildContentConversationArticleVideos builds the conversation allowing the user to select a video from the article
//   followed by each conversation which plays the video.
// initializes data for Tab.Options.
func _buildConversationArticleVideo(articleconversation, videosconvservation *conversation.Conversation) *conversation.Conversation {
	var conv *conversation.Conversation
	var ok bool
	var length int
	length = len(videosconvservation.Videos)
	ok = length > 0
	if ok {
        conv = conversation.NewConversation()
		if length == 1 {
			conv.Title = fmt.Sprintf("the video in %s.", articleconversation.Title)
			conv.Header.Append(fmt.Sprintf("This is the video in %s.", articleconversation.Title))
		} else {
			conv.Title = fmt.Sprintf("the first video in %s.", articleconversation.Title)
			conv.Header.Append(fmt.Sprintf("This is the first video in %s.", articleconversation.Title))
		}
		if setting.BrowserSettings.HelpLevel != setting.SettingHelpLevel3 {
			// a lot of help or a little help.
			// add helpful
			conv.Helpful = fmt.Sprintf("You may start and pause and restart the video using the space bar.")
		}
		if setting.BrowserSettings.HelpLevel == setting.SettingHelpLevel1 {
			// a lot of help
			// add more helpful
			conv.MoreHelpful = "You may say, yes, yes new tab, back, restart, finish, help, pause, or quit."
		}
		conv.Markup = videosconvservation.Videos[0].Markup
		conv.Type = conversation.ConversationTypeVideo
		conv.Footer = "That is the end of the video."
	} else {
    	// no videos
        conv = nil
    }
	return conv
}
